package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mmall.common.ServerResponse;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.ICategoryService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service("iCategoryService")
public class ICategoryServiceImpl  implements ICategoryService{

    private Logger logger = LoggerFactory.getLogger(ICategoryServiceImpl.class);

    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 添加品类
     * @param categoryName
     * @param parentId
     * @return
     */
    @Override
    public ServerResponse addCategory(String categoryName,Integer parentId) {
        if( parentId == null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("参加品类参数错误");
        }

        Category category = new Category();

        category.setName(categoryName);
        category.setParentId(parentId);
        //这个分类是可用的
        category.setStatus(true);

        int rowCount = categoryMapper.insert(category);

        if( rowCount > 0){
            return ServerResponse.createBySuccessMessage("添加品类成功");
        }
        return ServerResponse.createByErrorMessage("添加品类失败");

    }

    @Override
    public ServerResponse updateCategoryName(Integer categoryId,String categoryName ){
        if( categoryId == null || StringUtils.isBlank(categoryName)){
            return ServerResponse.createByErrorMessage("修改品类参数错误");
        }

        Category category = new Category();
        category.setId(categoryId);
        category.setName(categoryName);

        int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
        if( rowCount > 0){
            return  ServerResponse.createBySuccessMessage("修改品类名字成功");
        }
        return ServerResponse.createByErrorMessage("修改品类名字失败");
    }
    @Override
    public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){
        List<Category> categories = categoryMapper.selectCategoryChildrenByParentId(categoryId);

        if(CollectionUtils.isEmpty(categories)){
                logger.info("未找到当前分类的自分类");
        }
        return ServerResponse.createBySuccess(categories);
    }
    @Override
    public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){
        Set<Category> categorySet = Sets.newHashSet();
        findChidCategory(categorySet,categoryId);
        List<Integer> categoryid = Lists.newArrayList();

        if( categoryId != null ){
            for(Category category : categorySet ){
                categoryid.add(category.getId());
            }
        }
        return ServerResponse.createBySuccess(categoryid);
    }

    private Set<Category> findChidCategory(Set<Category> categorySet,Integer categoryId){

        Category category = categoryMapper.selectByPrimaryKey(categoryId);

        if( categorySet != null){
            categorySet.add(category);
        }

        List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
        for( Category category1 :  categoryList){
            findChidCategory(categorySet,category1.getId());
        }

        return categorySet;

    }

}
